<?php

/*
 * Action - 3+$
 *
 * Name a card. Reveal the top 3 of your deck.
 * Trash the maatches. Put the rest on top in any order
 * 
 * When you buy this, you may overpay for it. For each 1$ you overpaid,
 * look at the top card of your deck, trash it, discard it or put it back.
 *
 */

class Doctor extends Card implements iAction, iKingdom, iGuilds {

    public function __construct() {
        parent::__construct("Doctor", 3);
        $this->undoPlay = false;
        $this->undoBuy = false;
    }

    public function getDescription() {
        return array(
            "Name a card. Reveal the top 3 of your deck.",
            "Trash the maatches. Put the rest on top in any order",
            "",
            "When you buy this, you may overpay for it. For each 1$ you overpaid,",
            "look at the top card of your deck, trash it, discard it or put it back."
        );
    }

    public function buy(Game $game) {

        $card_options = array();
        $card_options[] = new Option(1, "Put on top");
        $card_options[] = new Option(1, "Trash it");
        $card_options[] = new Option(1, "Discard it");

        $overpay = $game->getOverpayAmount($this->getValue());
        for ($i = 0; $i < $overpay; $i++) {
            $card = $game->removeCardFromDrawDeck();
            if (!$card) {
                break;
            }
            ScreenDisplay::addScreenMessage($game, true, "look at " . nicePrint($card), "looks at 1 card");
            $output = MySocket::GetUserReply($game, Input_ChooseOne, $card_options, 1, 1, null, "What to do with " . $card->getValue() . "?");
            $res = $output[0];

            if ($res == "Put on top") {
                $returned_card = $game->putCardOnTopOfDrawDeck($card);
                ScreenDisplay::addScreenMessage($game, true, "return " . nicePrint($returned_card) . " on top of your deck", "returns 1 card on top of his deck");
            } elseif ($res == "Trash it") {
                $trashed_card = $game->putCardInTrash($card);
                ScreenDisplay::addScreenMessage($game, true, "trash " . nicePrint($trashed_card), "trashes " . nicePrint($trashed_card));
            } elseif ($res == "Discard it") {
                $discarded_card = $game->putCardInDiscardDeck($card);
                ScreenDisplay::addScreenMessage($game, true, "discard " . nicePrint($discarded_card), "discards 1 card ");
            }
        }
    }

    public function play(Game $game, $isRealCard) {

        $board_cards = $game->getAllBoardCards();
        ksort($board_cards);

        $output = MySocket::GetUserReply($game, Input_ChooseOne, $board_cards, 1, 1, null, "Name a card");

        $named_card = n2c($output[0]);
        ScreenDisplay::addScreenMessage($game, true, "name " . nicePrint($named_card), "names " . ScreenDisplay::$LMSG);

        $cards = $game->removeCardsFromDrawDeck(3);
        ScreenDisplay::addScreenMessage($game, true, "reveal " . nicePrint($cards), "reveals " . ScreenDisplay::$LMSG);
        $trashed_cards = array();
        $other_cards = array();
        foreach ($cards as $picked_card) {
            if ($picked_card->getKey() == $named_card->getKey()) {
                $trashed_cards[] = $picked_card;
            } else {
                $other_cards[] = $picked_card;
            }
        }
        ScreenDisplay::addScreenMessage($game, true, "trash " . nicePrint($trashed_cards), "trashes " . ScreenDisplay::$LMSG);
        foreach ($trashed_cards as $trashed_card) {
            $game->moveCardFromPileToPile($trashed_card, NOPILE, Trash);
        }

        if (sizeof($other_cards) > 0) {
            $unique = getUniqueCards($other_cards);
            if (sizeof($unique) > 1) {
                $output = MySocket::GetUserReply($game, Input_Reorder, $other_cards, -1, -1, null, "Choose the order of the cards");
                $output = array_reverse($output);
            } else {
                $output = $other_cards;
            }
            ScreenDisplay::addScreenMessage($game, true, "put " . nicePrint($output) . " back on your deck ", "puts " . sizeof($output) . " cards back on his deck");
            foreach ($output as $p) {
                $game->putCardOnTopOfDrawDeck($p);
            }
        }

        $game->cardActionFinished();
        return true;
    }

}

?>